%{
    This file is part of StemCellQC, a video bioinformatics software
    toolkit for analysis of phase contrast microscopy videos.
    Copyright 2013-2015 Vincent On. [vincenton001-at-gmail.com]

    StemCellQC is free software: you can redistribute it and/or 
    modify it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    StemCellQC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with StemCellQC.  If not, see <http://www.gnu.org/licenses/>.
%}

function varargout = mod_xls_gui(varargin)
% MOD_XLS_GUI MATLAB code for mod_xls_gui.fig
%      MOD_XLS_GUI, by itself, creates a new MOD_XLS_GUI or raises the existing
%      singleton*.
%
%      H = MOD_XLS_GUI returns the handle to a new MOD_XLS_GUI or the handle to
%      the existing singleton*.
%
%      MOD_XLS_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MOD_XLS_GUI.M with the given input arguments.
%
%      MOD_XLS_GUI('Property','Value',...) creates a new MOD_XLS_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before mod_xls_gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to mod_xls_gui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help mod_xls_gui

% Last Modified by GUIDE v2.5 29-Aug-2014 12:18:28

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @mod_xls_gui_OpeningFcn, ...
                   'gui_OutputFcn',  @mod_xls_gui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before mod_xls_gui is made visible.
function mod_xls_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to mod_xls_gui (see VARARGIN)

% Choose default command line output for mod_xls_gui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes mod_xls_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = mod_xls_gui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function remove_rows_Callback(hObject, eventdata, handles)
% hObject    handle to remove_rows (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of remove_rows as text
%        str2double(get(hObject,'String')) returns contents of remove_rows as a double


% --- Executes during object creation, after setting all properties.
function remove_rows_CreateFcn(hObject, eventdata, handles)
% hObject    handle to remove_rows (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in mod_xls.
function mod_xls_Callback(hObject, eventdata, handles)
% hObject    handle to mod_xls (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%% load data and labels
% get the directory
folder_name = uigetdir;
pastDir = cd;
cd( folder_name );

% the list of files in the directory
list = dir('*.csv');

if isempty(list)
    list = dir('*.xls');
end

%load all files in directory
hWaitbar = waitbar( 0, 'Loading data' );

%get dimensions
[~,~,temp] = xlsread( deblank( list(1).name ) );
[n,m] = size(temp);
last = n - 1;
%% Parameters
% remove_rows = 2:2:last_row;
new_folder = get( handles.folder_name, 'String' );

%%
data = cell(1);

mkdir(new_folder)

files = cell(size(list,1),1);

for i=1:size(list)
    remove_rows = eval( get( handles.remove_rows, 'String' ));
%     remove_rows = [2:8 10:16 18:24 26:32 34:40 42:48 50:56 58:64 66:72 74:80 82:88 90:96 98:104 106:112 114:120 122:128 130:136 138:144 146:152 154:160 162:168 170:176 178:184 186:192 194:200 202:208 210:216 218:224 226:232 234:240 242:248 250:256 258:264 266:272 274:280 282:288 290:296 298:304 306:312 314:320 322:328 330:336 338:344 346:352 354:360 362:368 370:376 378:384 386:392 394:400 402:408 410]

    remove_rows = remove_rows + 1
    
    files{i} = list(i,1).name;
    display( [num2str(i) ' - ' deblank( list(i).name )] ) ;
    
    [~,~,temp] = xlsread( deblank( list(i).name ) );

    temp(remove_rows, :) = [];
    
    data{i} = temp;
    file_name = strsplit(list(i).name,'.');
    xlswrite( [folder_name '\' new_folder '\' file_name{1}], temp);
    
    close( hWaitbar );
    
    hWaitbar = waitbar( i / length(list), ...
         'Loading data' );
end

handles.data = data;




set( handles.uitable1, 'data', files);
set( handles.data_menu, 'String', files);

file_num = get( handles.data_menu, 'Value');
set( handles.uitable2, 'data', data{file_num});

close( hWaitbar );

cd( pastDir );

guidata(hObject, handles);



function folder_name_Callback(hObject, eventdata, handles)
% hObject    handle to folder_name (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of folder_name as text
%        str2double(get(hObject,'String')) returns contents of folder_name as a double


% --- Executes during object creation, after setting all properties.
function folder_name_CreateFcn(hObject, eventdata, handles)
% hObject    handle to folder_name (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in data_menu.
function data_menu_Callback(hObject, eventdata, handles)
% hObject    handle to data_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data = handles.data;
file_num = get( handles.data_menu, 'Value');
set( handles.uitable2, 'data', data{file_num});


% Hints: contents = cellstr(get(hObject,'String')) returns data_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from data_menu


% --- Executes during object creation, after setting all properties.
function data_menu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to data_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called



% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
